package org.popupcommunication.controller;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.popupcommunication.exceptions.FileUploadException;
import org.popupcommunication.model.UploadedFile;
import org.popupcommunication.utils.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping(value =  "/public/uploadAvatar" )
public class UploadAvatarController {

	UploadedFile ufile;
	 
	   @RequestMapping(value = "/upload", method = RequestMethod.POST)
	   public @ResponseBody String upload(MultipartHttpServletRequest request, HttpServletResponse response, @ModelAttribute("UploadedFile") UploadedFile fichier) throws FileUploadException {                
	     //save image on disk
		MultipartFile file = fichier.getFile();  
		String avatarFolder = request.getSession().getServletContext().getRealPath("/resources/upload/")+ FileUtil.USER_AVATAR;
		try {
			//write
			FileUtil.uploadFile(file, avatarFolder,  fichier.getFile().getOriginalFilename());			
			//resize
			System.out.println(request.getSession().getServletContext().getResource("resources/upload"+ FileUtil.USER_AVATAR+"/"+fichier.getFile().getOriginalFilename()));
			BufferedImage bf = ImageIO.read(request.getSession().getServletContext().getResource("resources/upload"+ FileUtil.USER_AVATAR+"/"+fichier.getFile().getOriginalFilename()));
			BufferedImage imgResized =FileUtil.resizeImage(bf,bf.getType(),130, 130);
			ImageIO.write(imgResized, "jpg", new File(avatarFolder+"/"+fichier.getFile().getOriginalFilename())); 			
		} catch (Exception e) {
			e.printStackTrace();
			throw new FileUploadException(e.getMessage());
		}
	 
	     //return "<img src='"+request.getContextPath()+"/resources/upload"+FileUtil.USER_AVATAR+"/"+fichier.getFile().getOriginalFilename()+"' />";
	     return request.getContextPath()+"/resources/upload"+FileUtil.USER_AVATAR+"/"+fichier.getFile().getOriginalFilename();	 
	  }

  
		@RequestMapping(value = "/uploadImgGroup", method = RequestMethod.POST)
		public @ResponseBody
		String uploadImgGroup(MultipartHttpServletRequest request,
				HttpServletResponse response,
				@ModelAttribute("UploadedFile") UploadedFile fichier)throws Exception {
			// save image on disk
			MultipartFile file = fichier.getFile();
			String groupFolder = request.getSession().getServletContext()
					.getRealPath("/resources/upload/groupe/event");
			try {
				// write
				FileUtil.uploadFile(file, groupFolder, fichier.getFile()
						.getOriginalFilename());
				// resize
				BufferedImage bf = ImageIO.read(request
						.getSession()
						.getServletContext()
						.getResource(
								"resources/upload/groupe/event/" + fichier.getFile().getOriginalFilename()));
				BufferedImage imgResized = FileUtil.resizeImage(bf, bf.getType(),
						600, 400);
				ImageIO.write(imgResized, "jpg", new File(groupFolder + "/"
						+ fichier.getFile().getOriginalFilename()));
			} catch (FileUploadException e) {
				e.printStackTrace();
				throw new FileUploadException(e.getMessage());
			}

			// return
			// "<img src='"+request.getContextPath()+"/resources/upload"+FileUtil.USER_AVATAR+"/"+fichier.getFile().getOriginalFilename()+"' />";
			return request.getContextPath() + "/resources/upload/groupe/event/"
					+ fichier.getFile().getOriginalFilename();
		}	
		
		@ExceptionHandler(FileUploadException.class)
		public ModelAndView handleFileUploadException(FileUploadException ex) {
			ModelAndView model = new ModelAndView("errors/files_error");
			model.addObject("exception", ex);
			return model;
	 
		}		
}


 

 

